/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.forward.mobile.optitrans.service;

import com.forward.mobile.optitrans.db.DBSource;
import com.forward.mobile.optitrans.domain.Arret;
import com.forward.mobile.optitrans.domain.DeviceParam;
import com.forward.mobile.optitrans.domain.Position;
import com.forward.mobile.optitrans.util.StringCryptor;
import com.forward.mobile.optitrans.util.Utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONWriter;

/**
 *
 * @author jbromo
 */
public class AddDeviceLocation extends HttpServlet {

    private static final String KEY_LOCATION = "location";
    private static final String KEY_STATUS = "status";
    private static final String KEY_PARAM = "param";
    private static final String KEY_CONTENT = "text/html;charset=UTF-8";
    private static final int SUCCESS_VALUE = 1;
    

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType(KEY_CONTENT);
        PrintWriter out = response.getWriter();
        try {
            String argument = request.getParameter(KEY_LOCATION);
            //TODO Remove security and compression 
            //String value = Utils.decompress(StringCryptor.decrypt(argument));
            String[] item = argument.split("@");
            String uuid = item[0];
            String valueArret = item[1];
            String data = item[2];


            JSONWriter writer = new JSONWriter(out);
            DBSource dBSource = DBSource.getInstance();
            dBSource.open();
            DeviceParam deviceParam = dBSource.findDeviceParam(uuid);
            List<Position> positions = Utils.parseLocation(data);
            List<Arret> arret = Utils.parseArret(valueArret, deviceParam);
            dBSource.saveStartArret(arret);
            dBSource.updateOrInsertPosition(positions, deviceParam);
            writer.object();
            writer.key(KEY_STATUS).value(SUCCESS_VALUE);
            writer.key(KEY_PARAM).value(argument);
            writer.endObject();
        } catch (Exception exception) {
            exception.printStackTrace(out);
            exception.printStackTrace();
            JSONWriter writer = new JSONWriter(out);
            try {
                writer.object();

                writer.key(KEY_STATUS).value(exception.getLocalizedMessage());

                writer.endObject();
            } catch (JSONException ex) {
            }

        }


    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
